package org.jeecg.modules.amazon.amazon.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.CamelCaseConverter;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.amazon.amazon.entity.AmazonTask;
import org.jeecg.modules.amazon.amazon.service.IAmazonTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Description: 亚马逊采集任务表
 * @Author: jeecg-boot
 * @Date:   2025-11-08
 * @Version: V1.0
 */
@Tag(name="亚马逊采集任务表")
@RestController
@RequestMapping("/amazon/amazonTask")
@Slf4j
public class AmazonTaskController extends JeecgController<AmazonTask, IAmazonTaskService> {
	@Autowired
	private IAmazonTaskService amazonTaskService;

	/**
	 * 分页列表查询
	 *
	 * @param amazonTask
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "亚马逊采集任务表-分页列表查询")
	@Operation(summary="亚马逊采集任务表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AmazonTask>> queryPageList(AmazonTask amazonTask,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {


		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String id = loginUser.getId();
		String username = loginUser.getUsername();

		// 自定义查询规则
		Map<String, QueryRuleEnum> customeRuleMap = new HashMap<>();

		//管理员可以查全部  其他人只能查自己

		// 自定义多选的查询规则为：LIKE_WITH_OR
		customeRuleMap.put("taskType", QueryRuleEnum.LIKE_WITH_OR);
		customeRuleMap.put("taskSite", QueryRuleEnum.LIKE_WITH_OR);
		customeRuleMap.put("taskStatus", QueryRuleEnum.LIKE_WITH_OR);


		QueryWrapper<AmazonTask> queryWrapper = QueryGenerator.initQueryWrapper(amazonTask, req.getParameterMap(),customeRuleMap);
		if (StrUtil.isNotEmpty(username) && !username.equals("admin")){
			queryWrapper.eq("task_user_id",id);
		}
		Page<AmazonTask> page = new Page<AmazonTask>(pageNo, pageSize);
		IPage<AmazonTask> pageList = amazonTaskService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 接口列表查询
	 *
	 * @return
	 */
	//@AutoLog(value = "亚马逊采集任务表-分页列表查询")
	@IgnoreAuth
	@Operation(summary="亚马逊采集任务表-接口查询")
	@AutoLog(value = "亚马逊采集任务表-接口查询")
	@PostMapping(value = "/taskList")
	public Result<List<Map<String, Object>>> taskList(@RequestBody JSONObject jsonObject) {
		String user_id = jsonObject.getString("user_id");
		String gs_id = jsonObject.getString("gs_id");



		if (StrUtil.isEmpty(user_id)){
			return Result.error(500,"账号不能为空");
		}
		if (StrUtil.isEmpty(gs_id)){
			return Result.error(500,"租户id不能为空");
		}
		List<AmazonTask> amazonTasks= amazonTaskService.taskList(jsonObject);

		for (AmazonTask amazonTask : amazonTasks) {
			amazonTask.setTaskId(amazonTask.getId());
		}
		List<Map<String, Object>> maps = CamelCaseConverter.convertToCamelCaseMap(amazonTasks);
		return Result.OK(maps,1000,true);
	}
	/**
	 * 接口修改
	 *
	 * @return
	 */
	//@AutoLog(value = "亚马逊采集任务表-分页列表查询")
	@IgnoreAuth
	@Operation(summary="亚马逊采集任务表-接口修改 ")
	@AutoLog(value = "亚马逊采集任务表-接口修改")
	@PostMapping(value = "/finishTask")
	public Result finishTask(@RequestBody JSONObject jsonObject) {
		String task_id = jsonObject.getString("task_id");
		String task_status = jsonObject.getString("task_status");
		if (StrUtil.isEmpty(task_id)){
			return Result.error(500,"任务id不可为空");
		}
		if (StrUtil.isEmpty(task_status)){
			return Result.error(500,"任务状态不可为空");
		}
		AmazonTask amazonTask = new AmazonTask();
		amazonTask.setId(task_id);
		amazonTask.setTaskStatus(task_status);
		amazonTaskService.updateById(amazonTask);
		return Result.OK("更新成功！");
	}


	/**
	 *   添加
	 *
	 * @param amazonTask
	 * @return
	 */
	@AutoLog(value = "亚马逊采集任务表-添加")
	@Operation(summary="亚马逊采集任务表-添加")
	@RequiresPermissions("amazon:amazon_task:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AmazonTask amazonTask) {
		//当前登录的用户
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String id = loginUser.getId();
		String relTenantIds = loginUser.getRelTenantIds();
		amazonTask.setTaskUserId(id);
		if (StrUtil.isEmpty(relTenantIds)){
			amazonTask.setTaskGsId("");
		}else {
			String[] split = relTenantIds.split(",");
			amazonTask.setTaskGsId(split[0]);
		}
		amazonTaskService.save(amazonTask);

		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param amazonTask
	 * @return
	 */
	@AutoLog(value = "亚马逊采集任务表-编辑")
	@Operation(summary="亚马逊采集任务表-编辑")
	@RequiresPermissions("amazon:amazon_task:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AmazonTask amazonTask) {
		amazonTaskService.updateById(amazonTask);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "亚马逊采集任务表-通过id删除")
	@Operation(summary="亚马逊采集任务表-通过id删除")
	@RequiresPermissions("amazon:amazon_task:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		amazonTaskService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "亚马逊采集任务表-批量删除")
	@Operation(summary="亚马逊采集任务表-批量删除")
	@RequiresPermissions("amazon:amazon_task:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.amazonTaskService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "亚马逊采集任务表-通过id查询")
	@Operation(summary="亚马逊采集任务表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AmazonTask> queryById(@RequestParam(name="id",required=true) String id) {
		AmazonTask amazonTask = amazonTaskService.getById(id);
		if(amazonTask==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(amazonTask);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param amazonTask
	 */
	@RequiresPermissions("amazon:amazon_task:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, AmazonTask amazonTask) {
		return super.exportXls(request, amazonTask, AmazonTask.class, "亚马逊采集任务表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("amazon:amazon_task:importExcel")
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, AmazonTask.class);
	}

}
